//#include "./Rasterizer/triangle.h"
//#include "./Rasterizer/rasterizer.h"

#include<QApplication>
#include<QDebug>
#include<QDir>
#include<iostream>
//#include<opencv2/imgproc/imgproc.hpp>
//#include<opencv2/highgui.hpp>
//#include<opencv2/imgcodecs/imgcodecs.hpp>
//#include<opencv2/core/core.hpp>



#include"./editor_win.h"


int main(int argc,char* argv[])
{
    QApplication app(argc, argv);
    QDir::setCurrent(app.applicationDirPath());  // 设置相对路径：是必需的、但能否改动？



    EditorWin *w = new EditorWin();
    w->show();


    return app.exec();
}



// CV TEST
/*
using namespace cv;
int main(int argc,char* argv[])
{
    qDebug()<<1;
    QApplication a(argc, argv);
    Eigen::Matrix4f v = Eigen::Matrix4f::Identity();

    VideoCapture cap;
    cap.open(0);
    qDebug()<<cap.open(0);
    while (1) {
        Mat frame;
        cap >> frame;
        imshow("1",frame);
        if(waitKey(10)>=0)
            break;
    }
    return 0;
}
*/
/*
// Rasterizer
constexpr double MY_PI = 3.1415926;

// view transformation
Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
{
    Eigen::Matrix4f view = Eigen::Matrix4f::Identity();

    Eigen::Matrix4f translate;
    translate << 1, 0, 0, -eye_pos[0],
        0, 1, 0, -eye_pos[1],
        0, 0, 1, -eye_pos[2],
        0, 0, 0, 1;

    view = translate * view;

    return view;
}

// model transformation
Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
    float a = rotation_angle / 180.0 * MY_PI ;
    Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
    Eigen::Matrix4f translate;
    translate << cos(a),-sin(a),0,0,
                sin(a),cos(a),0,0,
                0,0,1,0,
                0,0,0,1;
    model = translate * model;
    return model;
}

// TODO to complete
//Eigen::Matrix4f get_rotation(Vector3f axis,float angle){
//    float a = angle /180.0 * MY_PI;
//    Eigen::Matrix4f I = Eigen::Matrix4f::Identity();
//    Eigen::Matrix4f rotation;
//    Eigen::Matrix4f n;
//    n << 0,-axis[2],axis[1],
//        axis[2],0,-axis[0],
//        -axis[1],axis[0],0;
//    rotation = (I*cos(a)) + (axis * axis.transpose()*(1-cos(a)) ) + n*sin(a);

//    return rotation;
//}

Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
                                      float zNear, float zFar)
{
    // Students will implement this function
    float a = eye_fov /180.0 * MY_PI ;
    float height = zNear * tan(a) * 2;
    //float wideht = height * aspect_ratio;
    auto t = -zNear * tan(a/2);
    auto b = -t;
    auto r = t * aspect_ratio;
    auto l = -r;
    Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
    Eigen::Matrix4f persp2ortho(4,4);
    Eigen::Matrix4f ortho(4,4);
    Eigen::Matrix4f ortho_translate(4,4);
    Eigen::Matrix4f ortho_scale(4,4);

    persp2ortho <<
        zNear,0,0,0,
        0,zNear,0,0,
        0,0,zNear+zFar,-zNear*zFar,
        0,0,1,0;
    ortho << 2/(r-l) ,0, -(r+l)/(r-l), 0,
             0 ,2/(t-b), -(t+b)/(t-b), 0,
             0 ,0, 2/(zNear-zFar), -2*zNear*zFar/(zNear-zFar),
             0,0,0,1;

    // ortho = ortho_scale * ortho_translate;
    projection = ortho * persp2ortho * projection;


    return projection;
}


int main(int argc,char* argv[])
{
    float angle = 0;
    bool command_line = false;
    std::string filename = "output.png";

    if (argc >= 3) {
        command_line = true;
        angle = std::stof(argv[2]); // -r by default
        if (argc == 4) {
            filename = std::string(argv[3]);
        }
        else
            return 0;
    }

    rst::Rasterizer r(700, 700);

    Eigen::Vector3f eye_pos = {0, 0, 5};    // camera pos

    std::vector<Eigen::Vector3f> pos{{2, 0, -2}, {0, 2, -2}, {-2, 0, -2}};

    std::vector<Eigen::Vector3i> ind{{0, 1, 2}};

    auto pos_id = r.load_positions(pos);
    auto ind_id = r.load_indices(ind);

    int key = 0;
    int frame_count = 0;

    if (command_line) {
        r.clear(rst::Buffers::Color | rst::Buffers::Depth);

        r.set_model(get_model_matrix(angle));
        r.set_view(get_view_matrix(eye_pos));
        r.set_projection(get_projection_matrix(45, 1, 0.1, 50));

        r.draw(pos_id, ind_id, rst::Primitive::Triangle);
        cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
        image.convertTo(image, CV_8UC3, 1.0f);

        cv::imwrite(filename, image);

        return 0;
    }

    while (key != 27) {
        r.clear(rst::Buffers::Color | rst::Buffers::Depth);

        r.set_model(get_model_matrix(angle));
        r.set_view(get_view_matrix(eye_pos));
        r.set_projection(get_projection_matrix(45, 1, 0.1, 50));

        r.draw(pos_id, ind_id, rst::Primitive::Triangle);

        cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
        image.convertTo(image, CV_8UC3, 1.0f);
        cv::imshow("image", image);
        key = cv::waitKey(10);

        std::cout << "frame'count: " << frame_count++ << '\n';

        if (key == 'a') {
            angle += 10;
        }
        else if (key == 'd') {
            angle -= 10;
        }
    }

    return 0;
}
*/
